package com.soft.bams.dao;

import com.soft.bams.entity.Account;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/22
 */
public class FileDao implements IDao<Account, List<Account>> {

    // 存储下一个ID
    File idFile = new File("id.txt");
    // 存储所有Account账户信息至文件
    File accFile = new File("account.txt");

    public FileDao() {
        // 若id.txt不存在，则先创建文件
        if (!idFile.exists()) {
            try {
                idFile.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        // 若account.txt不存在，则先创建文件
        if (!accFile.exists()) {
            try {
                accFile.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    // -------------------------id.txt文件 存取ID账户-------------------------
    // 读数据：从id.txt文件中读取ID  BufferedReader
    @Override
    public Long getId() {
        try (
                BufferedReader br = new BufferedReader(new FileReader(idFile))
        ) {
            String s = br.readLine();
            // 第一次读取文本，赋值初始值
            if (s == null || s.equals("")) {
                s = "100000000000000000";
            }
            Long id = Long.valueOf(s);
            return id;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 写数据：从内存中写入ID至磁盘文件id.txt文件  PrintWriter
    @Override
    public void setId(Long id) {
        try (
                PrintWriter pw = new PrintWriter(idFile);
        ) {
            pw.println(id);
            pw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    // -------------------------id.txt文件 存取ID账户-------------------------


    // -----------------account.txt文件 存取账户Account信息-----------------
    // 针对于List<Account>账户信息  写  序列化   ObjectOutputStream
    public void writeAccs(List<Account> accounts) {
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(accFile))
        ) {
            oos.writeObject(accounts);
            oos.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 针对于List<Account>账户信息  读  反序列化 ObjectInputStream
    public List<Account> readAccs() {
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(accFile))
        ) {
            List<Account> accounts = (List<Account>) ois.readObject();
            return accounts;
        } catch (IOException | ClassNotFoundException e) {
            return new ArrayList<>();
        }
    }

    // 插入 写
    @Override
    public void insert(Account account) {
        List<Account> accounts = readAccs();
        accounts.add(account);
        writeAccs(accounts);
    }

    // 查询 读
    @Override
    public Account selectOne(Long id, String password) {
        List<Account> accounts = readAccs();
        for (Account acc : accounts) {
            if (acc.getId().equals(id) && acc.getPassword().equals(password)) {
                return acc;
            }
        }
        return null;
    }

    // 查询 读
    @Override
    public Account selectOne(Long id) {
        List<Account> accounts = readAccs();
        for (Account acc : accounts) {
            if (acc.getId().equals(id)) {
                return acc;
            }
        }
        return null;
    }

    @Override
    public List<Account> selectAll() {
        return readAccs();
    }

    // 更新 写
    @Override
    public boolean update(Account account) {
        List<Account> accounts = readAccs();
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts.get(i).getId().equals(account.getId())) {
                accounts.set(i, account);
                break;
            }
        }
        writeAccs(accounts);
        return true;
    }
    // -----------------account.txt文件 存取账户Account信息-----------------
}